package edu.gyc.data.struct.stack;

public class CacluArrayStack {
    public static void main(String[] args) {
        String expression = "300+10*20-40";
        MyStack numberStack = new MyStack(10);
        MyStack operStack = new MyStack(10);
        int index = 0;
        int n1 = 0, n2 = 0;
        int oper = 0;
        int r = 0;
        char ch = ' ';
        String keepCh = ""; //拼接多位数的用
        while (true) {
            ch = expression.substring(index, index + 1).charAt(0);
            if (operStack.isOper(ch)) {//如果是运算符
                if (!operStack.isEmpty()) {
                    //进行优先级比较，若当前运算符优先级小于等于栈顶运算符，就pop两个数，
                    //从符号栈pop一个符号，运算，入数栈
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        n1 = numberStack.pop();
                        n2 = numberStack.pop();
                        oper = operStack.pop();
                        r = numberStack.cal(n1, n2, oper);
                        numberStack.push(r);
                        operStack.push(ch);
                    } else {
                        operStack.push(ch);
                    }
                } else {
                    operStack.push(ch);
                }
            } else {
                // numberStack.push(ch-48); //字符数字变对应整数
                //1 多位数时，不能发现是一个数就入栈，须要往后看，直到找到符号才能入栈；
                keepCh += ch;

                //如果ch是最后一位数，直接入栈
                if (index == expression.length() - 1) {
                    numberStack.push(Integer.parseInt(keepCh));
                } else {
                    //判断下一个字符是否是数字，是继续扫描，若是运算符，则入栈
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numberStack.push(Integer.parseInt(keepCh));
                        keepCh = "";
                    }
                }
            }
            index++;
            if (index >= expression.length()) {
                break;
            }
        }
        while (true) {
            //若符号栈为空，则数栈就剩一个数字，就是结果
            if (operStack.isEmpty()) {
                break;
            }
            n1 = numberStack.pop();
            n2 = numberStack.pop();
            oper = operStack.pop();
            r = numberStack.cal(n1, n2, oper);
            numberStack.push(r);
        }
        System.out.println("表达式：" + expression + "=" + numberStack.pop());
    }
}

class MyStack {
    private int maxSize;
    private int[] stack;
    private int top = -1;

    public MyStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    //栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int n) {
        if (isFull()) {
            System.out.println("栈满了，无法加入数据");
            return;
        }
        top++;
        stack[top] = n;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，无数据可弹出");
        }
        int t = stack[top];
        top--;
        return t;
    }

    public void show() {
        if (isEmpty()) {

            System.out.println("栈空，无数据可显示");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }

    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈空，无数据可弹出");
        }
        return stack[top];
    }

    //返回运算符优先级，数字越大优先级越高
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }


    public boolean isOper(int oper) {
        return oper == '+' || oper == '-' || oper == '*' || oper == '/';
    }

    public int cal(int n1, int n2, int oper) {
        int r = 0;
        switch (oper) {
            case '+':
                r = n1 + n2;
                break;
            case '-':
                r = n2 - n1;//注意顺序
                break;
            case '*':
                r = n1 * n2;
                break;
            case '/':
                r = n2 / n1;
                break;
        }
        return r;
    }

}

